package com.classloader;

/**
 * 修改class文件，暂时只提供修改常量池常量的功能
 * @author WLZ
 *
 */
public class ClassModifier {
	
	/**
	 * Class文件中常量池的起始偏移
	 */
	private static final int CONSTANT_POOL_COUNT_INDEX = 8 ;
	
	/**
	 * CONSTANT_Utf8_info常量的tag标志
	 */

	private static final int CONSTANT_Utf8_info = 1;
	
	/***
	 * 常量池中11种常量所占的长度，CONSTANT_Utf8_info型常量除外，因为他不是定长的
	 */
	private static final int[] CONSTANT_ITEM_LENGTH= {-1,-1,-1,5,5,9,9,3,3,5,5,5,5};
	
	private static final int u1 = 1;
	
	private static final int u2 = 2;
	
	private byte[] classByte;
	
	public ClassModifier(byte[] classByte) {
		this.classByte = classByte;
	}
	
	/**
	 * 修改常量池中的CONSTANT_Utf8_info常量的内容 
	 * @param oldStr 修改前字符串
	 * @param newStr 修改后字符串
	 * @return 修改结果
	 */
	public byte[] modifyUTF8Constant(String oldStr,String newStr) {
		int cpc = getConstantPoolCount();
		int offset = CONSTANT_POOL_COUNT_INDEX + u2;
		for (int i = 0; i < cpc; i++) {
			int tag = ByteUtils.bytes2Int(classByte,offset+u1,u2);
			if (tag == CONSTANT_Utf8_info) {
				offset +=(u1+u2);
			//	String str = ByteUtils.bytes2String
			}
		}
		
		return classByte;
	}

	private int getConstantPoolCount() {
		// TODO Auto-generated method stub
		return 0;
	}
	
}
